package com.miplus.ccrm.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.credit.*;
import com.miplus.ccrm.core.controller.vo.ficase.FiImpReqVo;
import com.miplus.ccrm.core.controller.vo.ficase.FiImpResVo;
import com.miplus.ccrm.core.mapper.ConCustomerMapper;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.crd.*;
import com.miplus.ccrm.core.service.cust.ConCustomerGrpTagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 2-4.CRD案件服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/crdt")
public class CreditController {
    @Resource
    private FiCreditcasesService fiCreditcasesService;
    @Resource
    private CreditCardService creditCardService;
    @Resource
    private CreditAccountService creditAccountService;
    @Resource
    private CreditAccountAmortService creditAccountAmortService;
    @Resource
    private FiCreditcasesHistService fiCreditcasesHistService;
    @Resource
    private FiTaskService fiTaskService;
    @Resource
    private CreditAccountBillhistService creditAccountBillhistService;
    @Resource
    private FiImportHistService fiImportHistService;
    @Resource
    private JCommonData jCommonData;
    @Resource
    private FiTaskOldhistService fiTaskOldhistService;
    @Resource
    private CreditAccountEventDtlService creditAccountEventDtlService;
    @Resource
    private CreditAccountNoutbillService creditAccountNoutbillService;
    @Resource
    private ConCustomerMapper customerMapper;
    @Resource
    private ConCustomerAccountService conCustomerAccountService;
    @Resource
    private IouLoanInvoiceInfoService iouLoanInvoiceInfoService;

    @Resource
    private IouRepayPlanService iouRepayPlanService;

    @Resource
    private IouRepaySerialService iouRepaySerialService;

    @Resource
    private IouCaseCsBillService iouCaseCsBillService;

    @Resource
    private IouCaseCsBillCancelService iouCaseCsBillCancelService;

    @Resource
    private FiCreditcaseGrpTagService fiCreditcaseGrpTagService;
    @Resource
    private ConCustomerGrpTagService conCustomerGrpTagService;
    @Resource
    private FiCreditcasesTagService fiCreditcasesTagService;

    @Resource
    private FiCreditcaseAssistantService fiCreditcaseAssistantService;
    /**
     * 取案件池GRID
     */
    @PostMapping("/crdCaseList")
    public R<PageData<CrdCaseResVo>> crdCaseList(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo){
        JSysUser user = jCommonData.getCurrUsr();

        IPage<CrdCaseResVo> quyResult = fiCreditcasesService.crdCaseList(user, crdCaseReqVo,true);

        String key = "";
        Boolean b = false;
        //if(JPubUtils.IsEqual(crdCaseReqVo.getIsdistquy(),"1")){//2024-06-09 add
            //在待分案查询条件需要保存，为了分案时取
            key=jCommonData.getDistQuyKey(user.getUsrid());
            //有效期2小时
            b = jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setDistQuyKey({},{}):ret={}",key,crdCaseReqVo,b);
        //}
        //else if(JPubUtils.IsEqual(crdCaseReqVo.getIsrecyquy(),"1")){//2024-07-28 add
            //在案件池一键回收查询条件需要保存，为了回收时取
            key=jCommonData.getRecyQuyKey(user.getUsrid());
            //有效期2小时
            b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setRecyQuyKey({},{}):ret={}",key,crdCaseReqVo,b);

            key=jCommonData.getAssQuyKey(user.getUsrid());
        //有效期2小时
            b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setAssQuyKey({},{}):ret={}",key,crdCaseReqVo,b);
        //}
            key=jCommonData.getStopCaseKey(user.getUsrid());
            //有效期2小时
            b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration);
            log.info("crdCaseList.setStopCaseKey({},{}):ret={}",key,crdCaseReqVo,b);
        //}
        if(JPubUtils.IsEqual(crdCaseReqVo.getIsUpSign(),"1")){
            // 记录上案、下案
            //取下一条数据条件及数据
            key=jCommonData.getQuyNextCaseKey(user.getUsrid());
            //有效期2小时
            b=jCommonData.setData(key,crdCaseReqVo,JPubUtils.distExpiration*2);
            log.info("caseList.SetQuyNextCaseKey({},{}):ret={}",key,crdCaseReqVo,b);

            key=jCommonData.getQuyNextCaseDataKey(user.getUsrid());
            b=jCommonData.setData(key,quyResult,JPubUtils.distExpiration*2);
            log.info("caseList.SetQuyNextCaseDataKey({}):ret={}",key,b);
        }

        return R.SuccessPage(quyResult);
    }

    /**
     * 取案件池GRID 金额字段统计
     */
    @PostMapping("/crdCaseListStatistics")
    public R<CrdCaseStatisticsResVo> crdCaseListStatistics(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.crdCaseListStatistics(user,crdCaseReqVo));
    }

    /**
     * 取查询案件欠款汇总金额（已出账单欠款）
     */
    @PostMapping("/getQueryDebtSum")
    public R<CrdQuySumVo> getQueryDebtSum(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.getQueryDebtSum(user,crdCaseReqVo));
    }

    /**
     * 取案件详细信息
     */
    @GetMapping("/crdCaseInfo")
    public R<CrdCaseInfoVo> crdCardList(@RequestParam @NotNull String ficaseid,
                                        @RequestParam String popup,
                                        @RequestParam(required = false) String disthistid){
        JSysUser user = jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.getCrdCaseInfo(user,Integer.parseInt(ficaseid),popup,disthistid));
    }

    /**
     * 通过案件id 获取客户姓名
     * @return
     */
    @GetMapping("/getCustNameByCaseId")
    public R<String>  getCustnameByCaseId(@RequestParam @NotNull Integer ficaseid){
//        JSysUser user = jCommonData.getCurrUsr();
        return  R.Success(customerMapper.getCustnameByCaseId(ficaseid));
    }

    /**
     * 取案件其他详细信息
     */
    @GetMapping("/crdCaseExtraInfo")
    public R<CrdCaseExtraInfo> crdCaseExtraInfo(@RequestParam @NotNull String ficaseid){
        //JSysUser user = jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.getCrdCaseExtraInfo(Integer.parseInt(ficaseid)));
    }
    /**
     * 获取企业信息
     * @param customernumber
     * @return
     */
    @GetMapping("/companyList")
    public R<List<ConCompanyResVo>>companyList(@RequestParam @NotNull String customernumber){
        JSysUser user = jCommonData.getCurrUsr();

        return R.Success(fiCreditcasesService.companyList(user,customernumber));
    }


    /**
     * 取案件账户信息GRID
     */
    @GetMapping("/crdAccountList")
    public R<PageData<CrdAccResVo>> crdAccountList(@Validated CrdAccReqVo crdAccReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(creditAccountService.crdAccountList(user,crdAccReqVo));
    }

//    /**
//     * 取客户卡片信息GRID
//     */
//    @GetMapping("/crdCardList")
//    public R<PageData<CrdCardResVo>> crdCardList(@Validated CrdCardReqVo crdCardReqVo){
//        JSysUser user = jCommonData.getCurrUsr();
//        return R.SuccessPage(creditCardService.crdCardList(user,crdCardReqVo));
//    }

    /**
     * 取客户卡片信息GRID
     */
    @GetMapping("/crdCardList")
    public R<PageData<ConCustomerAccountResVo>> crdCardList(@Validated CrdCardReqVo crdCardReqVo){
        JSysUser user = jCommonData.getCurrUsr();

        return R.SuccessPage(conCustomerAccountService.accountPage(user,crdCardReqVo));
    }

    /**
     * 取账单分期信息GRID
     */
    @GetMapping("/crdAmortList")
    public R<PageData<CrdAmortResVo>> crdAmortList(@Validated CrdAmortReqVo crdAmortReqVo){
        return R.SuccessPage(creditAccountAmortService.crdAmortList(crdAmortReqVo));
    }

    /**
     * 获取借据信息GRID
     */
    @GetMapping("/iouLoanInvoiceList")
    public R<PageData<LoanInvoiceListResVo>> iouLoanInvoiceList(@Validated LoanInvoiceListReqVo loanInvoiceListReqVo){
        return R.SuccessPage(iouLoanInvoiceInfoService.loanInvoiceList(loanInvoiceListReqVo));
    }

    /**
     * 获取借据还款计划信息GRID
     */
    @GetMapping("/iouLoanRepayPlanList")
    public R<PageData<IouRepayPlan>> iouLoanRepayPlanList(@Validated LoanRepayReqVo loanRepayReqVo){
        return R.SuccessPage(iouRepayPlanService.getRepayPlanList(loanRepayReqVo));
    }

    /**
     * 获取借据还款明细信息GRID
     */
    @PostMapping("/selectRepaidList")
    public R<PageData<LoanRepayResVo>> selectRepaidList(@RequestBody @Validated LoanRepayReqVo loanRepayReqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(iouRepaySerialService.selectRepaidList(user,loanRepayReqVo));
    }

    /**
     * 获取借据还款明细信息GRID
     */
    @GetMapping("/iouLoanRepaidList")
    public R<PageData<IouRepaySerial>> iouLoanRepaidList(@Validated LoanRepayReqVo loanRepayReqVo){
        return R.SuccessPage(iouRepaySerialService.getRepaidList(loanRepayReqVo));
    }

    /**
     * 还款明细历史导出
     */
    @PostMapping("/expRepaidList")
    public R<CmdResVo> expRepaidList(@RequestBody @Validated LoanRepayReqVo paras, HttpServletResponse response){
        //String usrid= jCommonData.getCurrUsrID();
        JSysUser sysUser = jCommonData.getCurrUsr();
        return R.Success(iouRepaySerialService.expRepaidList(sysUser, paras,response));
    }

    /**
     * 还款明细历史导出
     */
    @PostMapping("/confirmRepaySerial")
    public R<CmdResVo> confirmRapaySerial(@RequestBody @Validated IouRepaySerial iouRepaySerial){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(iouRepaySerialService.updateRapySerial(usrid, iouRepaySerial));
    }
    /**
     * 获取借据清单信息(法诉)
     */
    @PostMapping("/caseCsBillPage")
    public R<PageData<CaseCsBillPageResVo>> caseCsBillPage(@RequestBody @Validated CaseCsBillPageReqVo reqVo){
        return R.SuccessPage(iouCaseCsBillService.caseCsBillPage(reqVo));
    }

    /**
     * 撤诉和取消分发借据信息(法诉)
     */
    @PostMapping("/caseCsBillCancelPage")
    public R<PageData<CaseCsBillCancelPageResVo>> caseCsBillCancelPage(@RequestBody @Validated CaseCsBillPageReqVo reqVo){
        return R.SuccessPage(iouCaseCsBillCancelService.caseCsBillCancelPage(reqVo));
    }


    /**
     * 取分案历史GRID
     */
    @GetMapping("/fiCaseDistList")
    public R<PageData<DistListResVo>> fiCaseDistList(@Validated DistListReqVo distListReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCaseDistList(user,distListReqVo));
    }

    /**
     * 取分案历史明细GRID
     */
    @PostMapping("/fiCaseDistDetailList")
    public R<PageData<DistDetailListResVo>> fiCaseDistDetailList(@RequestBody @Validated DistDetailListReqVo distDetailListReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCaseDistDetailList(user,distDetailListReqVo,true));
    }
    /**
     * 取分案历史明细GRID  导出
     */
    @PostMapping("/expCaseDistDetailList")
    public R<CmdResVo> expCaseDistDetailList(@RequestBody @Validated DistDetailListReqVo distDetailListReqVo,HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesHistService.expCaseDistDetailList(user,distDetailListReqVo,response));
    }

    /**
     * 取分案批次明细（按机构）
     */
    @GetMapping("/fiCaseDistBatchDetailList")
    public R<PageData<DistListResVo>> fiCaseDistBatchDetailList(@Validated DistGrpBtcTailReqVo distGrpBtcTailReqVo){
        return R.SuccessPage(fiCreditcasesHistService.fiCaseDistBatchDetailList(distGrpBtcTailReqVo));
    }

    /**
     * 取催记历史GRID
     */
    @PostMapping("/taskHist")
    public R<PageData<FiTaskResVo>> taskHist(@RequestBody @Validated FiTaskReqVo fiTaskReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiTaskService.taskHist(user,fiTaskReqVo));
    }

    /**
     * 催记历史导出
     */
    @PostMapping("/expTaskHist")
    public R<CmdResVo> expTaskHist(@RequestBody @Validated FiTaskReqVo fiTaskReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiTaskService.saveAsExcel(user,fiTaskReqVo,response));
    }

    /**
     * 增加外呼催记
     */
    @PostMapping("/addDialTask")
    public R<CmdResVo> addDialTask(@RequestBody @Validated TaskCallAddVo taskCallAddVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.addDialTask(usrid,taskCallAddVo));
    }

    /**
     * 保存外呼结果催记
     */
    @PostMapping("/saveDialTask")
    public R<CmdResVo> saveDialTask(@RequestBody @Validated TaskCallSaveVo taskCallSaveVo){
        String usrid= jCommonData.getCurrUsrID();
        //修改 20250508 先更新task，后更新案件表。
        CmdResVo  cmdResVo =    fiTaskService.saveDialTask(usrid,taskCallSaveVo);
        if( cmdResVo.getCount() != 1){
            return R.Success(cmdResVo);
        }
        Integer tasknumner = Integer.parseInt(cmdResVo.getKeyId());

        fiCreditcasesService.updateCallTask(
                taskCallSaveVo.getFicaseid(),
                tasknumner,
                taskCallSaveVo.getAppointdt()
        );
        return R.Success(cmdResVo);
        //return R.Success(fiTaskService.saveDialTask(usrid,taskCallSaveVo));
    }

    /**
     * 增加短信催记
     */
    @PostMapping("/addSmsTask")
    public R<CmdResVo> addSmsTask(@RequestBody @Validated TaskSmsAddVo taskSmsAddVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.addSmsTask(usrid,taskSmsAddVo));
    }

    /**
     * 保存短信发送结果催记
     */
    @PostMapping("/saveSmsTask")
    public R<CmdResVo> saveSmsTask(@RequestBody @Validated TaskSmsSaveVo taskSmsSaveVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.saveSmsTask(usrid,taskSmsSaveVo));
    }

    /**
     * 取组对应案件数LIST
     */
    @PostMapping("/grpCaseCountList")
    public R<List<GrpCaseCountVo>> grpCaseCountList(@RequestBody @Validated GrpListReqVo grpListReqVo){
        return R.Success(fiCreditcasesService.grpCaseCountList(grpListReqVo));
    }

    /**
     * 取案件当前进行的催记状态
     */
    @GetMapping("/getCurrentTask")
    public R<List<FiTaskCurrVo>> getCurrentTask(@RequestParam @NotNull Integer ficaseid){
        return R.Success(fiTaskService.getCurrentTask(ficaseid));
    }

    /**
     * 催记置顶
     */
    @PostMapping("/fiTaskTopSet")
    public R<CmdResVo> fiTaskTopSet(@RequestBody @Validated FiTaskTopVo fiTaskTopVo){
        return R.Success(fiTaskService.taskTopSet(fiTaskTopVo.getTasknumber(),fiTaskTopVo.getToppos()));
    }

    /**
     * 催记置顶取消
     */
    @PostMapping("/fiTaskTopCancel")
    public R<CmdResVo> fiTaskTopCancel(@RequestBody @Validated FiTaskTopVo fiTaskTopVo){
        return R.Success(fiTaskService.taskTopCancel(fiTaskTopVo.getTasknumber()));
    }

    /**
     * 分配案件到部门或机构
     */
    @PostMapping("/fiCaseDistribute")
    public R<List<DistResVo>> fiCaseDistribute(@RequestBody @Validated DistReqVo distReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(distReqVo.getIsquydist(),"1")){//2024-06-09 一键分案
            String key=jCommonData.getDistQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseDistribute:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
            }
        }
        return R.Success(fiCreditcasesService.fiCaseDistribute(user,distReqVo,quyparas));
    }

    /**
     * 分配案件到催员(派工)
     */
    @PostMapping("/fiCaseAssgin")
    public R<List<DistResVo>> fiCaseAssgin(@RequestBody @Validated AssReqVo assReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(assReqVo.getIsquydist(),"1")){//2024-06-09 一键分案
            String key="";
            if(JPubUtils.IsNotEmpty(assReqVo.getGrpid())){//直接派工
                key=jCommonData.getDistQuyKey($usrid);
            }else {
                key=jCommonData.getAssQuyKey($usrid);
            }
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseAssgin:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
            }
        }
        return R.Success(fiCreditcasesService.fiCaseAssign(user,assReqVo,quyparas));
    }

    /**
     * 回收案件
     */
    @PostMapping("/fiCaseRecycle")
    public R<CmdResVo> fiCaseRecycle(@RequestBody @Validated RecyReqVo recyReqVo){

        JSysUser user= jCommonData.getCurrUsr();
        String $usrid= user.getUsrid();
        CrdCaseReqVo quyparas=null;
        if(JPubUtils.IsEqual(recyReqVo.getIsquyrecy(),"1")){//2024-07-28 一键回收
            String key=jCommonData.getRecyQuyKey($usrid);
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseRecycle:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
            }
        }
        return R.Success(fiCreditcasesService.fiCaseRecycle(user,recyReqVo,quyparas));
    }

    /**
     * 取客户关联的案件数量
     */
    @GetMapping("/getCustCaseCount")
    public R<CmdResVo> getCustCaseCount(@RequestParam @NotNull String custnumber){
        return R.Success(fiCreditcasesService.getCustFiCaseCount(custnumber));
    }

    /**
     * 取客户下最新激活的卡号
     */
    @GetMapping("/getCustOpenLastCardNo")
    public R<CmdResVo> getCustOpenLastCardNo(@RequestParam @NotNull String custnumber){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(creditCardService.getCustOpenLastCardNo(user,custnumber));
    }

    /**
     * 取账户下最新激活的卡号
     */
    @GetMapping("/getAccOpenLastCardNo")
    public R<CmdResVo> getAccOpenLastCardNo(@RequestParam @NotNull String acctno){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(creditCardService.getAccOpenLastCardNo(user,acctno));
    }

    /**
     * 取案件下的减免申请数量
     */
    @GetMapping("/getFiCaseReliefCount")
    public R<CmdResVo> getFiCaseReliefCount(@RequestParam @NotNull Integer ficaseid){
        return R.Success(fiCreditcasesService.getFiCaseReliefCount(ficaseid));
    }

    /**
     * 卡冻结指令
     */
    @PostMapping("/crdCardFrozen")
    public R<CrdCmdResVo> crdCardFrozen(@RequestBody @Validated CrdFrozenReqVo crdFrozenReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.crdCardFrozen(usrid,crdFrozenReqVo));
    }

    /**
     * 卡划扣指令
     */
    @PostMapping("/crdCardDeduction")
    public R<CrdCmdResVo> crdCardDeduction(@RequestBody @Validated CrdDeduReqVo crdDeduReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.crdCardDeduction(usrid,crdDeduReqVo));
    }

    /**
     * 查询借记卡余额
     */
    @GetMapping("/getCrdAcctBalance")
    public R<CrdCmdResVo> getCrdAcctBalance(@RequestParam @NotNull Integer ficaseid,String cardno){
        FiCreditcases fi=fiCreditcasesService.getById(ficaseid);
        String paycardno=cardno;
        if(JPubUtils.IsEmpty(cardno)||cardno.indexOf("*")>=0){
            ConCustomer cu=customerMapper.selectById(fi.getCustomernumber());
            paycardno=cu.getDebitcardno();
        }
        return R.Success(CrdCommand.crdAcctBalance(paycardno));
    }

    /**
     * 取历史账单Grid
     */
    @GetMapping("/getCrdAcctBillHist")
    public R<PageData<CrdBillHistResVo>> getCrdAcctBillHist(@Validated CrdBillHistReqVo crdBillHistReqVo){
        return R.SuccessPage(creditAccountBillhistService.crdBillHistList(crdBillHistReqVo));
    }

    /**
     * 取今日预约Grid
     */
    @GetMapping("/fiAppointCaseList")
    public R<PageData<FiAppointResVo>> fiAppointCaseList(@Validated FiAppointReqVo fiAppointReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.SuccessPage(fiCreditcasesService.fiAppointCaseList(usrid,fiAppointReqVo));
    }

    /**
     * 导入离线催记
     */
    @PostMapping("/impTaskList")
    public R<ImpResultVo> impTaskList(@RequestBody @Validated FiTaskImpVo fiTaskImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.impTaskList(usrid,fiTaskImpVo));
    }

    /**
     * 导入历史催记
     */
    @PostMapping("/impHistTaskList")
    public R<ImpResultVo> impHistTaskList(@RequestBody @Validated HistTaskImpVo histTaskImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.impHistTaskList(usrid,histTaskImpVo));
    }

    /**
     * 查询历史催记Grid
     */
    @PostMapping("/viewHistTask")
    public R<PageData<HistTaskResVo>> viewHistTask(@RequestBody @Validated HistTaskReqVo histTaskReqVo){
        return R.SuccessPage(fiTaskOldhistService.viewHistTask(histTaskReqVo));
    }

    /**
     * 历史催记导出
     */
    @PostMapping("/expHistTask")
    public R<CmdResVo> expHistTask(@RequestBody @Validated HistTaskReqVo histTaskReqVo, HttpServletResponse response){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskOldhistService.expHistTask(usrid,histTaskReqVo,response));
    }

    /**
     * 导入催收函/诉讼/支付令
     */
    @PostMapping("/impTaskCmdList")
    public R<ImpResultVo> impTaskCmdList(@RequestBody @Validated TaskCmdImpVo taskCmdImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.impTaskCmdList(usrid,taskCmdImpVo));
    }

    /**
     * 删除导入数据批次
     */
    @PostMapping("/delImpTaskList")
    public R<CmdResVo> delImpTaskList(@RequestBody @Validated TaskBatchDelVo taskBatchDelVo){
        return R.Success(fiTaskService.delImpTaskList(taskBatchDelVo));
    }

    /**
     * 取批次导入/导出历史GRID
     */
    @GetMapping("/fiImportBatchList")
    public R<PageData<FiImpResVo>> fiImportBatchList(@Validated FiImpReqVo fiImpReqVo){
        return R.SuccessPage(fiImportHistService.fiImportBatchList(fiImpReqVo));
    }

    /**
     * 刷新账户余额信息
     */
    @PostMapping("/crdRefreshBalance")
    public R<CrdCmdResVo> crdRefreshBalance(@RequestBody @Validated CrdRefreshBalVo crdRefreshBalVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.refreshBalance(user,crdRefreshBalVo));
    }

    /**
     * 刷新未出账单信息
     */
    @PostMapping("/crdRefreshNoOutBill")
    public R<CrdCmdResVo> crdRefreshNoOutBill(@RequestBody @Validated CrdRefNOutBillVo crdRefNOutBillVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.refreshNoOutBill(user,crdRefNOutBillVo));
    }

    /**
     * 取历史账单明细GRID
     */
    @GetMapping("/crdOutBillList")
    public R<PageData<CrdBillTailResVo>> crdOutBillList(@Validated CrdBillTailReqVo crdBillTailReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(creditAccountEventDtlService.crdEventBillList(user,crdBillTailReqVo));
    }

    /**
     * 取案件产品类型
     */
    @GetMapping("/getCrdProdType")
    public R<CmdResVo> getCrdProdType(@RequestParam @NotNull Integer ficaseid){
        return R.Success(fiCreditcasesService.getProdType(ficaseid));
    }

    /**
     * 取未出账单
     */
    @GetMapping("/crdNOutBill")
    public R<CrdNOutBillResVo> crdNOutBill(@RequestParam @NotNull String  acctno){
        return R.Success(creditAccountNoutbillService.crdNOutBill(acctno));
    }

    /**
     * 取未出账单明细GRID
     */
    @GetMapping("/crdNOutBillList")
    public R<PageData<CrdNOBillTailResVo>> crdNOutBillList(@Validated CrdNOBillTailReqVo crdNOBillTailReqVo){
        return R.SuccessPage(creditAccountNoutbillService.crdNOutBillList(crdNOBillTailReqVo));
    }

    /**
     * 佣金计算
     */
    @PostMapping("/crdCommisList")
    public R<PageData<CommisResVo>> crdCommisList(@RequestBody @Validated CommisReqVo commisReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcasesHistService.fiCommisList(user,commisReqVo));
    }

    /**
     * 查询AI推送名单
     */
    @GetMapping("/queryAiList")
    public R<PageData<AiListResVo>> queryAiList(@Validated AiListReqVo aiListReqVo){
        return R.SuccessPage(creditAccountService.queryAiList(aiListReqVo));
    }

    /**
     * 推送AICall
     */
    @PostMapping("/sendAiList")
    public R<CmdResVo> sendAiList(@RequestBody @Validated AiListSendVo aiListSendVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.sendAiList(usrid,aiListSendVo));
    }

    /**
     * 回写AICall结果
     */
    @PostMapping("/saveAiListResult")
    public R<CmdResVo> saveAiListResult(@RequestBody @Validated AiCallRetVo aiCallRetVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(fiTaskService.saveAiListResult(usrid,aiCallRetVo));
    }

    /**
     * 取客户下的案件LIST.ID
     */
    @GetMapping("/getFiCaseIDList")
    public R<List<Integer>> getFiCaseIDList(@RequestParam @NotNull String custno){
        return R.Success(fiCreditcasesService.getCaseIDList(custno));
    }

    /**
     * 案件导出
     */
    @PostMapping("/expFiCase")
    public R<CmdResVo> expFiCase(@RequestBody @Validated CrdCaseReqVo crdCaseReqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.saveAsExcel(user,crdCaseReqVo,response));
    }

    /**
     * 取一键回收数据进程
     */
    @GetMapping("/getRecycleProcess")
    public R<CmdResVo> getRecycleProcess(){
        return R.Success(fiCreditcasesService.getRecycleProcess());
    }

    /**
     * 取一键回收案件数
     */
    @GetMapping("/getOneRecyCount")
    public R<CmdResVo> getOneRecyCount(){
        JSysUser user= jCommonData.getCurrUsr();
        String key=jCommonData.getRecyQuyKey(user.getUsrid());
        Object o=jCommonData.getData(key);
        CrdCaseReqVo quyparas=null;
        if(o==null){
            log.warn("getOneRecyCount:nofind {}",key);
            return R.Error(EnumRCode.NoFindQuyPara);
        }else{
            quyparas=(CrdCaseReqVo)o;
        }
        return R.Success(fiCreditcasesService.getOneRecyCount(user,quyparas));
    }

    /**
     * 案件标签维护
     * @param reqVo
     * @return
     */
    @PostMapping("/caseTag")
    public R<CmdResVo> caseTag(@RequestBody @Validated CaseTagReqVo reqVo){
        JSysUser usr= jCommonData.getCurrUsr();

        if(ObjectUtils.isNotNull(reqVo.getIsassquy()) && reqVo.getIsassquy() == 1) {
            String key = jCommonData.getStopCaseKey(usr.getUsrid());
            //有效期2小时
            Object o = jCommonData.getData(key);
            if (o == null) {
                log.warn("fiDelayAdd:nofind {}", key);
                return R.Error(EnumRCode.NoFindQuyPara);
            } else {
                CrdCaseReqVo quyparas = (CrdCaseReqVo) o;
                List<Integer> crdList = new ArrayList<>();
                int cnt = fiCreditcasesService.bindFiCastList(crdList, usr, quyparas);
                if (cnt > 0)
                    reqVo.setCaseId(crdList);
            }
        }
        return R.Success(fiCreditcasesTagService.caseTag(reqVo));
    }


    /**
     * 获取案件分组标签LIST
     * @return
     */
    @GetMapping("/getCaseGrpTagList")
    public R<List<LabelResVo>>getCaseGrpTagList(){
        return R.Success(fiCreditcaseGrpTagService.getCaseGrpTagList());
    }
    /**
     * 获取客户分组标签LIST
     * @return
     */
    @GetMapping("/getCustGrpTagList")
    public R<List<LabelResVo>>getCustGrpTagList(){
        return R.Success(conCustomerGrpTagService.getCustGrpTagList());
    }


    /**
     * 获取案件下的借据产品信息
     * @return
     */
    @GetMapping("/getInvoiceGrp")
    public R<String>getInvoiceGrp(@RequestParam("creditcaseid") @NotNull String creditcaseid){

        return iouLoanInvoiceInfoService.getInvoiceGrp(creditcaseid);
    }


    /**
     * 案件详细取下一案件ID
     */
    @GetMapping("/getNextFiCaseID")
    public R<CmdResVo> getNextFiCaseID(@RequestParam @NotNull Integer ficaseid){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.getNextFiCaseID(user,ficaseid));
    }

    /**
     * 案件详细取上一案件ID
     */
    @GetMapping("/getPrevFiCaseID")
    public R<CmdResVo> getPrevFiCaseID(@RequestParam @NotNull Integer ficaseid){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(fiCreditcasesService.getPrevFiCaseID(user,ficaseid));
    }

    /**
     * 协催查询
     */
    @PostMapping("/assistantList")
    public R<PageData<AssistListResVo>> assistantList(@RequestBody @Validated AssistListReqVo assistListReqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.SuccessPage(fiCreditcaseAssistantService.assistantList(user, assistListReqVo));
    }

    /**
     * 添加协催员。
     * @param reqVo
     * @return
     */
    @PostMapping("/addAssistant")
    public R<CmdResVo> addAssistant(@RequestBody @Validated CrdAssistantReqVo reqVo){

        JSysUser user= jCommonData.getCurrUsr();
        CrdCaseReqVo quyparas = null;
        //处理语句
        if(ObjectUtils.isNotNull(reqVo.getIsassquy()) && reqVo.getIsassquy() == 1){
            String key=jCommonData.getStopCaseKey(user.getUsrid());
            //有效期2小时
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseAssist:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
                List<Integer> crdList = new ArrayList<>();
                int cnt = fiCreditcasesService.bindFiCastList(crdList,user,quyparas);
                if(cnt > 0)
                    reqVo.setFicaseids(crdList);
            }
        }
        return R.Success(fiCreditcaseAssistantService.addAssistant(user.getUsrid(), reqVo));
    }


    /**
     * 删除催员。
     * @param reqVo
     * @return
     */
    @PostMapping("/delAssistant")
    public R<CmdResVo> delAssistant(@RequestBody @Validated CrdAssistantReqVo reqVo){

        JSysUser user= jCommonData.getCurrUsr();
        CrdCaseReqVo quyparas = null;
        //处理语句
        if(ObjectUtils.isNotNull(reqVo.getIsassquy()) && reqVo.getIsassquy() == 1){
            String key=jCommonData.getStopCaseKey(user.getUsrid());
            //有效期2小时
            Object o=jCommonData.getData(key);
            if(o==null){
                log.warn("fiCaseAssist:nofind {}",key);
                return R.Error(EnumRCode.NoFindQuyPara);
            }else{
                quyparas=(CrdCaseReqVo)o;
                List<Integer> crdList = new ArrayList<>();
                int cnt = fiCreditcasesService.bindFiCastList(crdList,user,quyparas);
                if(cnt > 0)
                    reqVo.setFicaseids(crdList);
            }
        }
        return R.Success(fiCreditcaseAssistantService.delAssistant(user.getUsrid(), reqVo));
    }

}
